import numpy as np 
import matplotlib.pyplot as plt 
import tensorflow as tf 

# build data
points_num = 100
vectors = []
# generate 100 points
# y = 0.1 * x + 0.2

for i in xrange(points_num):
    x1 = np.random.normal(0.0, 0.66)
    y1 = 0.1 * x1 + 0.2 + np.random.normal(0.0, 0.04)
    vectors.append([x1, y1])

x_data = [v[0] for v in vectors]
y_data = [v[1] for v in vectors]

# plt.plot(x_data, y_data, 'r*', label="Original data")
# plt.legend()
# plt.show()

# build linear regression model
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0)) #init Weight
b = tf.Variable(tf.random_uniform([1])) #init bias
y = W * x_data + b  #computed result by model

# define loss function
loss = tf.reduce_mean(tf.square(y - y_data))

# gradient decent 
# param1: learning rate
optimizer = tf.train.GradientDescentOptimizer(0.03) 
train = optimizer.minimize(loss)

# create session
sess = tf.Session()

# init data tensor
init = tf.initialize_all_variables()
sess.run(init)

# train steps
for step in xrange(200):
    sess.run(train)
    print("Step=%d, Loss=%f, [Weight=%f Bias=%f]" % (step, sess.run(loss), sess.run(W), sess.run(b)))

# fig2
plt.plot(x_data, y_data, 'r*', label="Original data")
plt.plot(x_data, sess.run(W) * x_data + sess.run(b), label="Fitted line")
plt.legend()
plt.show()

sess.close()



